using System;
using XDFLib.Extensions;
using System.Collections.Generic;


namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_BuildListFromConstant_RT : RT_Node
    {
        public RT_SingleValuePort MemberCount_RT;

        public RT_SingleValuePort Constant_RT;

        public RT_ValueListPort Result_RT;

        public Node_BuildListFromConstant_RT(Node_BuildListFromConstant sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            MemberCount_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.MemberCount, this);

            Constant_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Constant, this);

            Result_RT = new RT_ValueListPort(EPortIOType.Output, sourceData.Result, this);
            Result_RT.OnGetValueList = GetResult;
        }

        public List<AValue> GetResult(DAG_Executor executor)
        {
            int memberCount = MemberCount_RT.GetValue(executor).ToInteger().Item2;
            var constant = Constant_RT.GetValue(executor);

            memberCount = memberCount < 0 ? 0 : memberCount;
            var info = executor.GetExeInfoOfNode(this) as BuildListFromConstant_Info;
            if (info == null)
            {
                info = new BuildListFromConstant_Info() {
                    ValueList = new List<AValue>(memberCount)
                };
                executor.SetExeInfoOfNode(this, info);
            }

            var vList = info.ValueList;
            if (memberCount != vList.Count)
            {
                vList.Resize(memberCount, () => { return constant.Clone(); });
            }
            return info.ValueList;
        }
    }

    public class BuildListFromConstant_Info : RT_NodeExeInfo
    {
        public List<AValue> ValueList;
    }

}
